package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UserDataBean;
import com.xuxin.baby.contract.UserRechargeContract;
import com.xuxin.baby.event.UserFragmentEvent;
import com.xuxin.baby.popup.ConfirmPopup;
import com.xuxin.baby.presenter.UserRechargePresenter;
import com.xuxin.baby.utils.ACache;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.rxjava3.annotations.NonNull;

/**
 * Created by xu_xin
 * Created on 2020.08.07 0007
 * 充值
 */
public class UserRechargeActivity extends BaseActivity implements UserRechargeContract.View {
    private final UserRechargeContract.Presenter presenter = new UserRechargePresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.user_recharge_total)
    TextView user_recharge_total;
    @BindView(R.id.user_recharge_beans)
    EditText user_recharge_beans;
    @BindView(R.id.button)
    Button button;

    private String tenantId;//租户id
    private String token;//授权token
    private String memberId;//用户id

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_user_recharge);
    }

    @Override
    public void initView() {
        title_name.setText("充值");
        button.setText("充值");
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        memberId = aCache.getAsString(BaseConstant.memberId);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.button,
            R.id.user_recharge_beans_10, R.id.user_recharge_beans_20,
            R.id.user_recharge_beans_30, R.id.user_recharge_beans_50,
            R.id.user_recharge_beans_100, R.id.user_recharge_beans_200})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.button) {
            try {
                int beans = Integer.parseInt(user_recharge_beans.getText().toString().trim());
                if (beans > 0) {
                    recharge(beans);
                } else {
                    ToastUtils.showShort("请输入充值金额。");
                }
            } catch (Exception e) {
                e.printStackTrace();
                ToastUtils.showShort("请输入充值金额2。");
            }
        } else if (id == R.id.user_recharge_beans_10) {
            recharge(10);
        } else if (id == R.id.user_recharge_beans_20) {
            recharge(20);
        } else if (id == R.id.user_recharge_beans_30) {
            recharge(30);
        } else if (id == R.id.user_recharge_beans_50) {
            recharge(50);
        } else if (id == R.id.user_recharge_beans_100) {
            recharge(100);
        } else if (id == R.id.user_recharge_beans_200) {
            recharge(200);
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getUserData(tenantId, token, memberId);
            } else if (msg.what == 1) {
                presenter.recharge(tenantId, token, memberId, (int) msg.obj);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        ToastUtils.showShort("充值成功");
        EventBus.getDefault().post(new UserFragmentEvent());
        finish();
    }

    @Override
    public void onSuccess(UserDataBean userDataBean) {
        user_recharge_total.setText(String.valueOf("余额：" + userDataBean.getResult().getBeans()));
    }

    private void recharge(int rechargeBeans) {
        String message = "将要充值宝宝币" + rechargeBeans + "，确定要充值吗？";
        new XPopup.Builder(context)
                .dismissOnTouchOutside(true)
                .dismissOnBackPressed(true)
                .asCustom(new ConfirmPopup(context, message, new ConfirmPopup.OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = rechargeBeans;
                        popup.setTitle("正在充值中...").show();
                        handler.sendMessage(message);
                    }
                })).show();
    }
}